package com.mysite.seckill.commons.exception;

public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private BusinessError error;
    private Throwable throwable;

    public BusinessException() {
    }

    public BusinessException(int errorCode) {
        this.error = BusinessError.getError(errorCode);
    }

    public BusinessException(BusinessError error) {
        this.error = error;
    }

    public BusinessException(Throwable throwable) {
        this.throwable = throwable;
    }

    public BusinessException(BusinessError error, Throwable throwable) {
        this.error = error;
        this.throwable = throwable;
    }

    public BusinessError getError() {
        return error == null ? BusinessError.COMMON_ERROR : error;
    }

    public Throwable getThrowable() {
        return throwable;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (error != null) {
            sb.append("业务逻辑错误！" + System.lineSeparator());
            sb.append("错误代码：" + error.getErrorCode() + System.lineSeparator());
            sb.append("错误消息：" + error.getErrorMessage() + System.lineSeparator());
        }
        if (throwable != null) {
            sb.append(throwable.toString());
        }
        return sb.toString();
    }
}
